//
//  KooCalendarWeekView.m
//  KooIEDUCalendarDemo
//
//  Created by nowcoder on 2024.
//

#import "KooCalendarWeekView.h"
#import "KooCalendarConfig.h"
#import <Masonry/Masonry.h>

@interface KooCalendarWeekView ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) NSMutableArray<UILabel *> *mutableWeekLabels;

@end

@implementation KooCalendarWeekView

#pragma mark - Initialization

- (instancetype)init {
    return [self initWithConfig:[KooCalendarConfig defaultConfig]];
}

- (instancetype)initWithConfig:(KooCalendarConfig *)config {
    return [self initWithConfig:config weekTitles:[self.class defaultWeekTitles]];
}

- (instancetype)initWithWeekTitles:(NSArray<NSString *> *)weekTitles {
    return [self initWithConfig:[KooCalendarConfig defaultConfig] weekTitles:weekTitles];
}

- (instancetype)initWithConfig:(KooCalendarConfig *)config weekTitles:(NSArray<NSString *> *)weekTitles {
    self = [super init];
    if (self) {
        self.config = config ?: [KooCalendarConfig defaultConfig];
        self.weekTitles = weekTitles ?: [self.class defaultWeekTitles];
        [self setupUI];
        [self setupConstraints];
        [self updateAppearance];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.config = [KooCalendarConfig defaultConfig];
        self.weekTitles = [self.class defaultWeekTitles];
        [self setupUI];
        [self setupConstraints];
        [self updateAppearance];
    }
    return self;
}

#pragma mark - UI Setup

- (void)setupUI {
    self.backgroundColor = [UIColor clearColor];
    
    // 使用懒加载的方式添加UI组件，避免重复创建
    [self addSubview:self.containerView];
}

- (void)setupConstraints {
    // 容器视图约束 - 通过懒加载访问
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    // 星期标签约束 - 通过懒加载访问
    UILabel *previousLabel = nil;
    for (NSInteger i = 0; i < self.mutableWeekLabels.count; i++) {
        UILabel *weekLabel = self.mutableWeekLabels[i];
        
        [weekLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.containerView);
            make.bottom.equalTo(self.containerView);
            
            if (previousLabel) {
                make.left.equalTo(previousLabel.mas_right);
                make.width.equalTo(previousLabel);
            } else {
                make.left.equalTo(self.containerView);
            }
            
            if (i == self.mutableWeekLabels.count - 1) {
                make.right.equalTo(self.containerView);
            }
        }];
        
        previousLabel = weekLabel;
    }
}

- (void)updateAppearance {
    if (!self.config) return;
    
    // 更新所有星期标签的样式
    for (UILabel *weekLabel in self.mutableWeekLabels) {
        weekLabel.textColor = self.config.weekTitleColor;
        weekLabel.font = self.config.weekTitleFont;
    }
}

#pragma mark - Public Methods

- (void)updateWeekTitles:(NSArray<NSString *> *)weekTitles {
    if (!weekTitles || weekTitles.count != 7) return;
    
    // 直接设置实例变量，避免循环调用setter
    _weekTitles = weekTitles;
    
    // 更新标签文本
    for (NSInteger i = 0; i < MIN(weekTitles.count, self.mutableWeekLabels.count); i++) {
        UILabel *weekLabel = self.mutableWeekLabels[i];
        weekLabel.text = weekTitles[i];
    }
}

- (void)updateWithConfig:(KooCalendarConfig *)config {
    if (!config) return;
    
    self.config = config;
    [self updateAppearance];
}

- (void)setWeekLabelAtIndex:(NSInteger)index textColor:(UIColor *)textColor font:(UIFont *)font {
    if (index < 0 || index >= self.mutableWeekLabels.count) return;
    
    UILabel *weekLabel = self.mutableWeekLabels[index];
    if (textColor) {
        weekLabel.textColor = textColor;
    }
    if (font) {
        weekLabel.font = font;
    }
}

- (void)resetAllWeekLabelsStyle {
    [self updateAppearance];
}

#pragma mark - Utility Methods

+ (NSArray<NSString *> *)defaultWeekTitles {
    return @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
}

+ (NSArray<NSString *> *)englishWeekTitles {
    return @[@"Sun", @"Mon", @"Tue", @"Wed", @"Thu", @"Fri", @"Sat"];
}

+ (NSArray<NSString *> *)shortWeekTitles {
    return @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
}

#pragma mark - Getters

- (NSArray<UILabel *> *)weekLabels {
    return [self.mutableWeekLabels copy];
}

#pragma mark - Setters

- (void)setWeekTitles:(NSArray<NSString *> *)weekTitles {
    _weekTitles = weekTitles;
    
    // 如果UI已经创建，更新标签文本
    if (self.mutableWeekLabels.count > 0) {
        [self updateWeekTitles:weekTitles];
    }
}

- (void)setConfig:(KooCalendarConfig *)config {
    _config = config;
    
    // 如果UI已经创建，更新样式
    if (self.mutableWeekLabels.count > 0) {
        [self updateAppearance];
    }
}

#pragma mark - Lazy Loading

- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = [UIColor clearColor];
    }
    return _containerView;
}

- (NSMutableArray<UILabel *> *)mutableWeekLabels {
    if (!_mutableWeekLabels) {
        _mutableWeekLabels = [[NSMutableArray alloc] init];
        
        // 创建7个星期标签
        for (NSInteger i = 0; i < 7; i++) {
            UILabel *weekLabel = [[UILabel alloc] init];
            weekLabel.textAlignment = NSTextAlignmentCenter;
            weekLabel.backgroundColor = [UIColor clearColor];
            
            // 设置文本
            if (i < self.weekTitles.count) {
                weekLabel.text = self.weekTitles[i];
            }
            
            // 应用当前配置的样式
            if (self.config) {
                weekLabel.textColor = self.config.weekTitleColor;
                weekLabel.font = self.config.weekTitleFont;
            }
            
            [self.containerView addSubview:weekLabel];
            [_mutableWeekLabels addObject:weekLabel];
        }
    }
    return _mutableWeekLabels;
}

@end 
